const connection = require('../app/database')

class LableService {
  // 插入数据到数据库
  async create(...payload) {

    // 1.获取请求参数
    const [ name ] = payload

    // 2.定义预处理语句
    const statement = 'INSERT INTO `lable` ( `name`) VALUES (?);'

    // 3.执行sql语句
    let [res, fields] = await connection.execute(statement, [name])
    return res
  }

  async queryLables(...payload) {

    // 1.获取请求参数
    const [ offset,size ] = payload

    // 2.定义预处理语句
    const statement = `SELECT * FROM lable LIMIT ? OFFSET ?`

    // 3.执行sql语句
    let [res, fields] = await connection.execute(statement, [size,offset])
    return res
  }

  async queryLableByName(...payload) {

    // 1.获取请求参数
    const [ name ] = payload

    // 2.定义预处理语句
    const statement = 'SELECT * FROM lable WHERE name = ?;'

    // 3.执行sql语句
    let [res, fields] = await connection.execute(statement, [name])
    return res[0]
  }
}

module.exports = new LableService()